 
#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/core/ocl.hpp>
 
using namespace cv;
using namespace std;
 
int main()
{
	Mat frame;
	Rect2d roi;
	// Create a tracker 创建跟踪器
	Ptr<Tracker> tracker;
	//跟踪算法类型
	//string trackerTypes[7] = { "BOOSTING", "MIL", "KCF", "TLD","MEDIANFLOW", "MOSSE", "CSRT" };
	string videoName;
 
	while (true)
	{
		while (true)
		{
			cout << "\n\n------------------------------------------------------------------\n";
			cout << "\n\n>> 可测试算法有  BOOSTING  MIL  KCF  TLD  MEDIANFLOW  MOSSE  CSRT" 
					"\n>> 请输入要测试的算法并按回车,退出输入exit。"
					"\n>> ";
 
			string trackerType;
 
			cin >> trackerType;
 
			if (trackerType == "exit")
			{
				return 0;
			}
 
			if (trackerType == "BOOSTING")
			{
				tracker = TrackerBoosting::create();
				cout << "\n>> 选择BOODSTING成功！\n";
				break;
			}
			else if (trackerType == "MIL")
			{
				tracker = TrackerMIL::create();
				cout << "\n>> 选择MIL成功！\n";
				break;
			}
			else if (trackerType == "KCF")
			{
				tracker = TrackerKCF::create();
				cout << "\n>> 选择KCF成功！\n";
				break;
			}
			else if (trackerType == "TLD")
			{
				tracker = TrackerTLD::create();
				cout << "\n>> 选择TLD成功！\n";
				break;
			}
			else if (trackerType == "MEDIANFLOW")
			{
				tracker = TrackerMedianFlow::create();
				cout << "\n>> 选择MEDIANFLOW成功！\n";
				break;
			}
			else if (trackerType == "MOSSE")
			{
				tracker = TrackerMOSSE::create();
				cout << "\n>> 选择MOSSE成功！\n";
				break;
			}
			else if (trackerType == "CSRT")
			{
				tracker = TrackerCSRT::create();
				cout << "\n>> 选择CSRT成功！\n";
				break;
			}
			else
			{
				cout << "\n>> 选择失败\n";
				continue;
			}
		}
 
		while (true)
		{
			cout << "\n +--------------+"
					"\n | 1.步行的人_1 |"
					"\n | 2.步行的人_2 |"
					"\n | 3.步行的人_3 |"
					"\n | 4.车         |"
					"\n | 5.超车       |"
					"\n | 6.大卫       |"
					"\n | 7.跳绳       |"
					"\n | 8.摩托越野   |"
					"\n | 9.熊猫       |"
					"\n | 10.大众汽车  |"
					"\n +--------------+"
					"\n\n>> 请输入要播放视频的序列号（例如4）"
					"\n>> ";
 
			int videoNo;
			cin >> videoNo;
 
			if (videoNo == 1)
			{
				videoName = "pedestrian1.mpg";
				cout << "\n>> 选择《步行的人 1》成功！";
				break;
			}
			else if (videoNo == 2)
			{
				videoName = "pedestrian2.mpg";
				cout << "\n>> 选择《步行的人 2》成功！";
				break;
			}
			else if (videoNo == 3)
			{
				videoName = "pedestrian3.mpg";
				cout << "\n>> 选择《步行的人 3》成功！";
				break;
			}
			else if (videoNo == 4)
			{
				videoName = "/home/cy/workspace/YOLOX_deepsort_tracker/video0.mp4";
				cout << "\n>> 选择《车》成功！";
				break;
			}
			else if (videoNo == 5)
			{
				videoName = "carchase.mpg";
				cout << "\n>> 选择《超车》成功！";
				break;
			}
			else if (videoNo == 6)
			{
				videoName = "david.mpg";
				cout << "\n>> 选择《大卫》成功！";
				break;
			}
			else if (videoNo == 7)
			{
				videoName = "jumping.mpg";
				cout << "\n>> 选择《跳绳》成功！";
				break;
			}
			else if (videoNo == 8)
			{
				videoName = "motocross.mpg";
				cout << "\n>> 选择《摩托越野》成功！";
				break;
			}
			else if (videoNo == 9)
			{
				videoName = "panda.mpg";
				cout << "\n>> 选择《熊猫》成功！";
				break;
			}
			else if (videoNo == 10)
			{
				videoName = "volkswagen.mpg";
				cout << "\n>> 选择《大众汽车》成功！";
				break;
			}
			else
			{
				cout << "\n>> 序列号错误，请重新输入！";
				continue;
			}
		}
		// Read video 读视频
		VideoCapture video(videoName);
 
		// Exit if video is not opened 如果没有视频文件
		if (!video.isOpened())
		{
			cout << "\n>> 读取视频失败";
			continue;
		}
		cout << "\n +----------------------------+"
				"\n | 点击 c 逐帧播放视频        |"
				"\n | 点击 q 开始选择目标        |"
				"\n | 点击空格开始播放并追踪目标 |"
				"\n | 播放期间按q退出播放        |"
				"\n +----------------------------+\n";
		
		video >> frame;
		//! [getframe]
 
        cv::namedWindow("Tracking", 0);
		//added by Ujig
		while (1)
		{
			char key = waitKey(1);
			if (key == 'c')  // 按c键跳帧
			{
				video >> frame;
			}
			if (key == 'q')  // 按q键退出跳帧
			{
				break;
			}
			imshow("Tracking", frame);
		}
		cv::destroyWindow("Tracking");
        
		//end added
        cv::namedWindow("tracker", 0);
		//! [selectroi]选择目标roi以GUI的形式
		roi = selectROI("tracker", frame);
		//! [selectroi]
 
		//quit if ROI was not selected
		if (roi.width == 0 || roi.height == 0)
			return 0;
 
		// initialize the tracker
		//! [init]
		tracker->init(frame, roi);
		//! [init]
 
		printf("\n>> 开始播放\n");
        
		for (;; ) {
			// get frame from the video
			video >> frame;
 
			// stop the program if no more images
			if (frame.rows == 0 || frame.cols == 0)
				break;
 
			// update the tracking result
			//! [update]
			tracker->update(frame, roi);
			//! [update]
 
			//! [visualization]
			// draw the tracked object
			rectangle(frame, roi, Scalar(255, 0, 0), 2, 1);
 
			// show image with the tracked object
			imshow("tracker", frame);
			//! [visualization]
 
			if (char(waitKey(1)) == 'q') {
				break;
			}
		}
		cv::destroyWindow("tracker");
		cout << ">> 播放完毕\n";
	}
	return 0;
}